/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import framework.GameObject;
import framework.Key;
import framework.Screen;
import framework.Util;
import framework.gameAPI.Sprite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

/**
 *
 * @author edilson
 */
public class Tiro extends GameObject {

    private Sprite sprite;
    private static final int[] ANIM_TIRO1 = {0};
    private static final int[] ANIM_TIRO2 = {1};
    private static final int[] ANIM_TIRO3 = {2};
    private static final int[] ANIM_TIRO4 = {3};
    private static final int[] ANIM_TIRO5 = {4};
    private static final int[] ANIM_TIRO6 = {5};
    private static final int[] ANIM_DEAD = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    private int state;
    public static final int STATE_MOVING = 0;
    public static final int STATE_DYING = 1;
    public static final int STATE_DEAD = 2;
    private int type;
    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;
    public static final int TYPE_3 = 3;
    public static final int TYPE_4 = 4;
    public static final int TYPE_5 = 5;
    public static final int TYPE_6 = 6;
    private int cont;

    public Tiro(int x, int y, int type) {
        this.image = Util.loadImage(Global.IMG_TIROS);
        this.type = type;

        sprite = new Sprite(image, 25, 25);
        sprite.setAnimationDelay(5);
        this.x = x;
        this.y = y;
        state = STATE_MOVING;
        cont = 0;
        speedX = 0;


        switch (type) {
            case TYPE_1:
                speedY = -5;
                sprite.setFrameSequence(ANIM_TIRO1);
                this.width = 4;
                this.height = 12;
                this.power = 1;
                break;
            case TYPE_2:
                speedY = -7;
                sprite.setFrameSequence(ANIM_TIRO2);
                this.width = 9;
                this.height = 14;
                this.power = 3;
                break;
            case TYPE_3:
                speedY = -8;
                sprite.setFrameSequence(ANIM_TIRO3);
                this.width = 13;
                this.height = 14;
                this.power = 5;
                break;
            case TYPE_4:
                speedY = -6;
                sprite.setFrameSequence(ANIM_TIRO4);
                this.width = 16;
                this.height = 18;
                this.power = 8;
                break;
            case TYPE_5:
                speedY = -5;
                sprite.setFrameSequence(ANIM_TIRO5);
                this.width = 20;
                this.height = 20;
                this.power = 10;
                break;
            case TYPE_6:
                speedY = -1;
                sprite.setFrameSequence(ANIM_TIRO6);
                this.width = 21;
                this.height = 20;
                this.power = 15;
                break;
        }
    }

    @Override
    public void update() {

        switch (state) {

            case STATE_MOVING:
                cont++;
                if (power <= 0) {
                        state = STATE_DYING;
                        sprite.setFrameSequence(ANIM_DEAD);
                    }

                if (y < 0) {
                    visible = false;
                }

                if(this.type == TYPE_6 && speedY>-10&&cont%5==0)
                    speedY*=2;

                y += speedY;
                x+=speedX;
                break;
            case STATE_DYING:
                sprite.setAnimationDelay(3);
                if (sprite.animationEnded()) {
                    state = STATE_DEAD;
                }
                break;
            case STATE_DEAD:
                setVisible(false);
                break;
        }

    }

    public void bomm() {
        if (state != STATE_DYING) {
            state = STATE_DYING;
            sprite.setFrameSequence(ANIM_DEAD);
        }
    }

    @Override
    public void paint(Graphics g) {
        if (visible) {
            //g.drawImage(image, x, y, null);
            sprite.setX(x);
            sprite.setY(y);
            sprite.paint(g);
        }
    }

    public int getState() {
        return state;
    }


}
